<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>hello_react</title>
</head>
<body>
    <div id="test1"></div>

    <!-- 引入 react 核心库 -->
    <script src="./js/react.development.js" type="text/javascript"></script> <!-- React 对象 -->
    <script src="./js/react-dom.development.js" type="text/javascript"></script> <!-- ReactDOM 对象 -->
    <script src="./js/babel.min.js" type="text/javascript"></script> <!-- babel -->
    <script src="./js/prop-types.js" type="text/javascript"></script> <!-- PropTypes -->

    <script type="text/babel">
        class Life extends React.Component {
            state = {
                opacity: 1
            }
            death = () => {
                ReactDOM.unmountComponentAtNode(document.querySelector('#test1'))
            }
            // 组件挂载完毕调用
            componentDidMount() {
                this.timer = setInterval(() => {
                    let {opacity} = this.state
                    opacity -= 0.1
                    if (opacity <= 0) opacity = 1
                    this.setState({
                        opacity
                    })
                }, 200)
            }
            // 组件将要卸载时调用
            componentWillUnmount() {
                clearInterval(this.timer)
            }

            render() {
                return (
                    <div>
                        <h2 style={{opacity: this.state.opacity}}>React学不会怎么办?</h2>
                        <button onClick={this.death}>不活了</button>
                    </div>
                )
            }
        }
        ReactDOM.render(<Life/>, document.querySelector('#test1'))
    </script>
</body>
</html>